'use strict'

/**
 * <--    还原前    -->
 var a = !![],b = 'Hello ' + 'world' + '!',c = 2 + 3 * 50,d = Math.abs(-200) % 19,e = true ? 123:456;
 * 
 * <--    特点    -->
 * 变量声明中的初始化值都是一个表达式，且该表达式可以直接计算得值
 * 那么干脆直接用计算结果替换表达式，更易阅读
 * 
 * <--    解决办法    -->
 * 找到Expression，直接进行计算，如果正确就替换
 * 
 * <--    冗余判断    -->
 * 无
 * 
 * <--    还原后    -->
var a = true,
    b = "Hello world!",
    c = 152,
    d = 10,
    e = 123;
 * 
 **/

const {parse} = require('@babel/parser');
const traverse = require('@babel/traverse').default;
const t = require('@babel/types');
const generator = require('@babel/generator').default;

let code = `var a = !![],b = 'Hello ' + 'world' + '!',c = 2 + 3 * 50,d = Math.abs(-200) % 19,e = true ? 123:456;`

const visitor = {
    "BinaryExpression|ConditionalExpression|UnaryExpression|CallExpression|MemberExpression|SequenceExpression":{
        enter:function(path){
            let {confident, value} = path.evaluate();
            if(confident){
                path.replaceInline(t.valueToNode(value));
            }
        },
    },

}

let ast = parse(code);
traverse(ast, visitor);
console.log(generator(ast).code);

